<template>
  <el-dialog
    width="30%"
    title="发放金额"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="发放金额" prop="tradeAmount">
        <el-input type="number" v-model="dataForm.tradeAmount" placeholder="发放金额"></el-input>
      </el-form-item>

    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        driverId: null,
        tradeAmount: null,
        date: ''

      },
      dataRule: {
        tradeAmount: [
          {required: true, message: '金额不能为空', trigger: 'blur'}
        ]

      }
    }
  },
  methods: {
    init (id,date) {
      this.dataForm.driverId = id || 0
      this.dataForm.date = date
      this.visible = true
      this.$refs['dataForm'].resetFields()
    },
    // 表单提交
    dataFormSubmit () {
      if (this.dataForm.tradeAmount <= 0) {
        this.$message.error('发放金额必须大于0')
        return
      }
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/tms/salaryrecord/take`),
            method: 'post',
            data: this.$http.adornData({
              'driverId': this.dataForm.driverId,
              'tradeAmount': this.dataForm.tradeAmount,
              'date': this.dataForm.date
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
